<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="85px">
      <el-form-item label="初始化名称" prop="initName">
        <el-input
          v-model="queryParams.initName"
          placeholder="请输入初始化名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌名称" prop="brandName">
        <el-input
          v-model="queryParams.brandName"
          placeholder="请输入品牌名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入更新人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="是否默认" prop="qcIsDefault">
        <el-select v-model="queryParams.qcIsDefault" @change="handleQuery" placeholder="请选择是否默认" clearable size="small">
          <el-option
            v-for="(item, index) in qcIsDefaultOptions"
            :key="index"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['amz:productInit:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['amz:productInit:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['amz:productInit:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['amz:productInit:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ProductInitList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="初始化名称" align="center" prop="initName" :show-overflow-tooltip="true"/>
      <el-table-column label="品牌名称" align="center" prop="brandName" :show-overflow-tooltip="true"/>
      <el-table-column label="制造商" align="center" prop="manufacturer" :show-overflow-tooltip="true"/>
      <el-table-column label="商品编码类型" align="center" prop="externalProductIdType"
                       :formatter="externalProductIdTypeFormat"/>
      <!--      <el-table-column label="更新删除" align="center" prop="updateDelete" :formatter="updateDeleteFormat" />
            <el-table-column label="条件类型/状况" align="center" prop="conditionType" :formatter="conditionTypeFormat" />
            <el-table-column label="原产国/地区" align="center" prop="countryOfOrigin" :formatter="countryOfOriginFormat" />-->
      <el-table-column label="库存数量" align="center" prop="quantity"/>
      <el-table-column label="订单最大数量" align="center" prop="maxOrderQuantity"/>
      <el-table-column label="包裹数量" align="center" prop="packageContainsQuantity"/>
      <el-table-column label="是否默认" align="center" prop="qcIsDefault" :formatter="qcIsDefaultFormat" />


      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.qcIsDefault"
            active-value="true"
            inactive-value="false"
            @change="qcIsDefaultChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>

      <!--      <el-table-column label="商品标题前缀" align="center" prop="itemNamePrefix" />
            <el-table-column label="商品标题后缀" align="center" prop="itemNameSuffix" />
            <el-table-column label="商品描述前缀" align="center" prop="productDescriptionPrefix" />
            <el-table-column label="商品描述后缀" align="center" prop="productDescriptionSuffix" />
            <el-table-column label="运营中心编号" align="center" prop="fulfillmentCenterId" />-->
      <el-table-column label="创建人" align="center" prop="createBy"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['amz:productInit:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['amz:productInit:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改初始化商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="初始化名称" prop="initName">
          <el-input v-model="form.initName" placeholder="请输入初始化名称"/>
        </el-form-item>
        <el-form-item label="是否默认" prop="qcIsDefault">
          <el-select v-model="form.qcIsDefault" placeholder="请选择是否默认">
            <el-option
              v-for="dict in qcIsDefaultOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="品牌名称" prop="brandName">
          <el-input v-model="form.brandName" placeholder="请输入品牌名称"/>
        </el-form-item>
        <el-form-item label="制造商" prop="manufacturer">
          <el-input v-model="form.manufacturer" placeholder="请输入制造商"/>
        </el-form-item>
        <el-form-item label="商品编码类型" prop="externalProductIdType">
          <el-select v-model="form.externalProductIdType" placeholder="请选择商品编码类型">
            <el-option
              v-for="dict in externalProductIdTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="更新删除" prop="updateDelete">
          <el-select v-model="form.updateDelete" placeholder="请选择更新删除">
            <el-option
              v-for="dict in updateDeleteOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="条件类型/状况" prop="conditionType">
          <el-select v-model="form.conditionType" placeholder="请选择条件类型/状况">
            <el-option
              v-for="dict in conditionTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="原产国/地区" prop="countryOfOrigin">
          <el-select v-model="form.countryOfOrigin" placeholder="请选择原产国/地区">
            <el-option
              v-for="dict in countryOfOriginOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="库存数量" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入库存数量"/>
        </el-form-item>
        <el-form-item label="订单最大数量" prop="maxOrderQuantity">
          <el-input v-model="form.maxOrderQuantity" placeholder="请输入订单最大数量"/>
        </el-form-item>
        <el-form-item label="包裹数量" prop="packageContainsQuantity">
          <el-input v-model="form.packageContainsQuantity" placeholder="请输入包裹数量"/>
        </el-form-item>

        <el-form-item label="商品标题前缀" prop="itemNamePrefix">
          <el-input v-model="form.itemNamePrefix" placeholder="请输入商品标题前缀"/>
        </el-form-item>
        <el-form-item label="商品标题后缀" prop="itemNameSuffix">
          <el-input v-model="form.itemNameSuffix" placeholder="请输入商品标题后缀"/>
        </el-form-item>
        <el-form-item label="商品描述前缀" prop="productDescriptionPrefix">
          <el-input v-model="form.productDescriptionPrefix" type="textarea" placeholder="请输入内容"/>
        </el-form-item>

        <el-form-item label="商品描述后缀" prop="productDescriptionSuffix">
          <el-input v-model="form.productDescriptionSuffix" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="运营中心编号" prop="fulfillmentCenterId">
          <el-input v-model="form.fulfillmentCenterId" type="textarea" placeholder="请输入内容"/>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listProductInit,
    getProductInit,
    delProductInit,
    addProductInit,
    updateProductInit,
    exportProductInit,
    qcIsDefaultChange
  } from "@/api/amz/productInit";

  export default {
    name: "productInit",
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 初始化商品表格数据
        ProductInitList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,

        // 商品编码类型字典
        externalProductIdTypeOptions: [],
        // 更新删除字典
        updateDeleteOptions: [],
        // 条件类型/状况字典
        conditionTypeOptions: [],
        // 原产国/地区字典
        countryOfOriginOptions: [],
        // 是否默认字典
        qcIsDefaultOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          initName: null,
          brandName: null,
          updateBy: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          initName: [
            {required: true, message: "初始化名称不能为空", trigger: "blur"}
          ],
          brandName: [
            {required: true, message: "品牌名称不能为空", trigger: "blur"}
          ],
          manufacturer: [
            {required: true, message: "制造商不能为空", trigger: "blur"}
          ],
          externalProductIdType: [
            {required: true, message: "商品编码类型不能为空", trigger: "change"}
          ],
          updateDelete: [
            {required: true, message: "更新删除不能为空", trigger: "change"}
          ],
          conditionType: [
            {required: true, message: "条件类型/状况不能为空", trigger: "change"}
          ],
          countryOfOrigin: [
            {required: true, message: "原产国/地区不能为空", trigger: "change"}
          ],
          quantity: [
            {required: true, message: "库存数量不能为空", trigger: "blur"}
          ],
          maxOrderQuantity: [
            {required: true, message: "订单最大数量不能为空", trigger: "blur"}
          ],
          qcIsDefault: [
            {required: true, message: "是否默认不能为空", trigger: "blur"}
          ],
          createTime: [
            {required: true, message: "创建时间不能为空", trigger: "blur"}
          ],
          updateTime: [
            {required: true, message: "更新时间不能为空", trigger: "blur"}
          ],
        }
      };
    },
    created() {
      this.getList();

      this.getDicts("amz_external_product_id_type").then(response => {
        this.externalProductIdTypeOptions = response.data;
      });
      this.getDicts("amz_update_delete").then(response => {
        this.updateDeleteOptions = response.data;
      });
      this.getDicts("qc_is_boolean").then(response => {
        this.qcIsDefaultOptions = response.data;
      });
      this.getDicts("amz_condition_type").then(response => {
        this.conditionTypeOptions = response.data;
      });
      this.getDicts("amz_country_of_origin").then(response => {
        this.countryOfOriginOptions = response.data;
      });
    },
    methods: {
      /** 查询初始化商品列表 */
      getList() {
        this.loading = true;
        listProductInit(this.queryParams).then(response => {
          this.ProductInitList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 商品编码类型字典翻译
      externalProductIdTypeFormat(row, column) {
        return this.selectDictLabel(this.externalProductIdTypeOptions, row.externalProductIdType);
      },
      // 更新删除字典翻译
      updateDeleteFormat(row, column) {
        return this.selectDictLabel(this.updateDeleteOptions, row.updateDelete);
      },
      qcIsDefaultFormat(row, column) {
        return this.selectDictLabel(this.qcIsDefaultOptions, row.qcIsDefault);
      },
      // 条件类型/状况字典翻译
      conditionTypeFormat(row, column) {
        return this.selectDictLabel(this.conditionTypeOptions, row.conditionType);
      },
      // 原产国/地区字典翻译
      countryOfOriginFormat(row, column) {
        return this.selectDictLabel(this.countryOfOriginOptions, row.countryOfOrigin);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          productInitId: null,
          initName: null,
          brandName: null,
          manufacturer: null,
          externalProductIdType: null,
          updateDelete: null,
          conditionType: null,
          countryOfOrigin: null,
          quantity: null,
          maxOrderQuantity: null,
          packageContainsQuantity: null,
          itemNamePrefix: null,
          itemNameSuffix: null,
          productDescriptionPrefix: null,
          productDescriptionSuffix: null,
          fulfillmentCenterId: 'DEFAULT',
          qcIsDefault: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.productInitId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加初始化商品";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const productInitId = row.productInitId || this.ids
        getProductInit(productInitId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改初始化商品";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.productInitId != null) {
              updateProductInit(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addProductInit(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const productInitIds = row.productInitId || this.ids;
        this.$confirm('是否确认删除初始化商品编号为"' + productInitIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delProductInit(productInitIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有初始化商品数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportProductInit(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      },

      //是否默认变更事件
      qcIsDefaultChange(row) {
        let text = row.qcIsDefault === "true" ? "启用" : "停用";
        this.$confirm('确认要 【' + text + '】 当前记录吗?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {

          const reqDataExport = new FormData();
          reqDataExport.append("productInitId", row.productInitId);
          reqDataExport.append("qcIsDefault", row.qcIsDefault);
          return qcIsDefaultChange(reqDataExport);
        }).then(() => {
          this.msgSuccess(text + "成功");
          this.getList();
        }).catch(function() {
          row.qcIsDefault = row.qcIsDefault === "true" ? "false" : "true";
        });
      },

    }
  };
</script>
